<?php

namespace api\services\wechat;

use Yii;

class WechatMiniprogram implements WechatInterface
{

    const JS_TICKET_PATH = '/ticket/getticket';

    private $appId;
    private $secret;
    private $wechatAppId;
    private $wechatSecret;

    public function __construct($app = '', $channelId = '')
    {
        $configs = Yii::$app->params[$app];
        if (empty($configs) || (!empty($channelId) && empty($configs[$channelId]))) {
            $this->appId = WECHAT_MINIPROGRAM_APP_ID;
            $this->secret = WECHAT_MINIPROGRAM_SECRET;
            $this->wechatAppId = WECHAT_APP_ID;
            $this->wechatSecret = WECHAT_APP_SECRET;
            return;
        }
        $config = $configs[$channelId];
        $this->appId = $config['wechat_miniprogram_appId'];
        $this->secret = $config['wechat_miniprogram_secret'];
        $this->wechatAppId = $config['wechat_appId'];
        $this->wechatSecret = $config['wechat_secret'];
    }

    public function getHost()
    {
        return WECHAT_HOST;//'https://api.weixin.qq.com/cgi-bin';
    }

    public function getJsTicketAppId()
    {
        return $this->wechatAppId;
    }

    public function getJsTicketUrl($access_token)
    {
        return $this->getHost() . self::JS_TICKET_PATH . '?access_token=' . $access_token . '&type=jsapi';
    }

    public function getAccessTokenUrl($app = [])
    {
        return $this->getHost() . '/token?grant_type=client_credential&appid=' . $this->appId . '&secret=' . $this->secret;
    }

    public function getBindWechatAccessTokenUrl($app = [])
    {
        return $this->getHost() . '/token?grant_type=client_credential&appid=' . $this->wechatAppId . '&secret=' . $this->wechatSecret;
    }

    /**
     * @see https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
     * POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
     */
    public function sendSubscribeMessage($access_token, $template = [], $user = [], $eventData = [])
    {
        $params = [
            'touser' => $user['openId'] ?? 'oL6Uk5DFaniuvigvgfDqHNscv4BQ',
            'template_id' => $template['outerId'] ?? 'IDm6pxCK8OdY8V4-c2ZVUXWBZnlntrPY4mHCgFWVLSzhE',
            'page' => 'resolve/index',
            'data' => $this->formatData($eventData['event']),
        ];
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token}";

        return Yii::$app->curl->post($url, $params);
    }

    private function formatData($event)
    {
        /**
         * 工单号{{character_string9.DATA}}
         * 工单名称{{thing6.DATA}}
         * 工单备注{{thing7.DATA}}
         * 派单时间{{time22.DATA}}
         */
        return [
            'character_string9' => ['value' => $event['code']],
            'thing6' => ['value' => $event['name']],
            'thing7' => ['value' => $event['description']],
            'time22' => ['value' => date('Y-m-d H:i:s')],
        ];
    }
}
